package com.datang.pet.api.userController;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-06-11 22:17
 * @notify
 * @version 1.0
 */

import com.datang.pet.common.response.Result;
import com.datang.pet.common.utils.StringUtils;
import com.datang.pet.pojo.Pet;
import com.datang.pet.pojo.UserInfo;
import com.datang.pet.service.UserService;
import com.datang.pet.vo.ReadPet;
import com.datang.pet.vo.ReadUserInfo;
import com.datang.pet.vo.UpdateUserInfoAndAddUserAddressRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
public class UserLoginController {

    @Autowired
    private UserService userService;

    /*
     * @author 顶风少年
     * @date 2019/6/11 23:01
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param id
     * @param code
     * @result Result
     * @notify 获取用户信息
     * version 1.0
     */
    @GetMapping("/getUserInfo")
    public Result getUserInfo(String id, String code) throws Exception {

        if (StringUtils.isNUll(id) && StringUtils.isNUll(code)) {
            //userId和code都不等于空时
            throw new Exception("userId和code都有值！");
        } else if ((!StringUtils.isNUll(id)) && (!StringUtils.isNUll(code))) {
            //或者两者都为空时
            throw new Exception("传入的UserId和code的值为空！");
        } else {
            //获取用户信息
            UserInfo userInfo = userService.getUserIfNullAddUser(id, code);
            return Result.ok(userInfo);
        }
    }

    /*
     * @author 顶风少年
     * @date 2019/6/11 23:05
     * @mail dfsn199703132038@foxmail.com
     * @throws
     * @param
     * @param updateUserInfoAndAddUserAddressRequest
     * @result Result
     * @notify 修改头像昵称地理位置
     * version 1.0
     */
    @PostMapping("/updateUserAddress")
    public Result updateUserAddress(@RequestBody UpdateUserInfoAndAddUserAddressRequest updateUserInfoAndAddUserAddressRequest) throws Exception {
        if (!StringUtils.isNUll(updateUserInfoAndAddUserAddressRequest.getId())) {
            throw new Exception("用户id为空");
        }
        return Result.ok(userService.UpdateUserInfoAndAddUserAddress(updateUserInfoAndAddUserAddressRequest));
    }

    @GetMapping("/readUserInfo")
    public Result readUserInfo(String userId)throws Exception{
        ReadUserInfo readUserInfo = userService.readUserInfo(userId);
        List<ReadPet> readPetList = readUserInfo.getReadPetList();
        //当前时间
        Date nowTime = new Date();
        for (ReadPet p : readPetList) {
            Long i = (nowTime.getTime() - p.getPetBirthday().getTime())/(1000*60 * 60 * 24);
            if(i==0L){
                i=1L;
            }
            p.setShowDay(i);
        }

       return Result.ok(readUserInfo) ;
    }

}
